package org.example.mybatis.extend.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.mybatis.extend.bean.Animal;
import org.example.mybatis.extend.mapper.AnimalRepository;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Log4j2
@Transactional
public class AnimalService extends ServiceImpl<AnimalRepository, Animal> implements IAnimalService {
    //增
    public ResponseEntity<Animal> add(final Animal animal) {
        super.saveOrUpdate(animal);
        return ResponseEntity.ok(animal);
    }

    public ResponseEntity<Boolean> deleteById(final int id) {
        return ResponseEntity.ok(super.removeById(id));
    }

    //查
    public ResponseEntity<Animal> getById(final int id) {

        return ResponseEntity.ok((super.getById(id)));
    }
    //查

    public ResponseEntity<Animal> extendMethod(final int id) {
        return ResponseEntity.ok(getBaseMapper().extendMethod(id));
    }

    public Object query(String name, IPage<Animal> page) {
        QueryWrapper<Animal> conditions = Wrappers.query();
        conditions.like("name", name);
        return page(page, conditions);
    }
}
